package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //1.检查参数
        if (dto==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道参数为空");
        }
        //检查分页参数
        dto.checkParam();
        //2.分页查询
        IPage page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdChannel> lambdaQuery = Wrappers.lambdaQuery();
        //3.根据频道名称模糊查询,频道参数不为空才执行模糊搜索
        // lambdaQuery.like(获取对应的列名，需要查询的参数)
        if(StringUtils.isNotBlank(dto.getName())){
            lambdaQuery.like(AdChannel::getName,dto.getName());
        }
        //4.封装数据
        IPage result = this.page(page, lambdaQuery);
        PageResponseResult responseResult = new PageResponseResult();
        responseResult.setCurrentPage(dto.getPage());
        responseResult.setSize(dto.getSize());
        responseResult.setTotal(result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1.检查参数,记得检查传入进来的频道名称的空字符串
        if (adChannel==null||StringUtils.isBlank(adChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道添加参数为空");
        }
        //2.根据id查询频道参数
        int count = this.count(Wrappers.<AdChannel>lambdaQuery()
                                       .eq(AdChannel::getName,adChannel.getName()));
        //3.检查是否有同名的频道信息
        if (count>0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道已经存在不能继续添加");
        }
        //4.添加频道信息
        adChannel.setCreatedTime(new Date());
        save(adChannel);
        //5.返回结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1.检查参数
        if (adChannel==null||adChannel.getId()==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道修改参数为空");
        }
        //2.根据id查询是否有该频道信息
        AdChannel byId = getById(adChannel.getId());
        if (byId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"数据已经存在");
        }
        //3.先检查频道名称是否修改，再检查频道名称是否已经存在
        if (StringUtils.isNotBlank(adChannel.getName())&&!adChannel.getName().equals(byId.getName())) {
            int count = count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
            if (count>0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"数据已经存在");
            }
        }
        //4.修改频道信息
        this.updateById(adChannel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
        //1.检查参数
        if (id==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道删除参数为空");
        }
        //2.检索数据库是否存在对应的频道信息
        AdChannel byId = getById(id);
        if (byId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3.启用状态下不能删除
        if (byId.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_STATUS_TRUE);
        }
        //4.删除频道
        this.removeById(id);
        return ResponseResult.okResult();
    }
}
